﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmCustomerEdit : XtraForm
    {
        #region Properties
        private CustomerManager customerManager;
        public CustomerInfo Customer { get; set; }
       
        #endregion

        #region Init Form
        public frmCustomerEdit()
        {
            InitializeComponent();

            // Initialize all properties of this form
            this.customerManager = new CustomerManager();
            this.Customer = new CustomerInfo();
            
        }

        
        private void frmAuthorsEdit_Load_1(object sender, EventArgs e)
        {
            this.txtName.Text = this.Customer.Name;
            this.txtCode.Text = this.Customer.Code;
            this.txtLiabilites.Text = this.Customer.Liabilities.ToString();
            this.txtAddress.Text = this.Customer.Address;
            this.txtMobile.Text = this.Customer.Mobile;
            this.txtPhone.Text = this.Customer.Phone;
            this.txtEmail.Text = this.Customer.Email;
            this.txtFax.Text = this.Customer.Fax;
            this.txtTaxCode.Text = this.Customer.TaxCode;
            this.txtWebsite.Text = this.Customer.Website;
            this.txtDescription.Text = this.Customer.Description;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtContactor.Text = this.Customer.Contactor;
            this.txtContactorEmail.Text = this.Customer.ContactorEmail;
            this.txtContactorMobile.Text = this.Customer.ContactorMobile;
            this.txtContactorPhone.Text = this.Customer.ContactorPhone;
        }
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;
            this.txtCode.Properties.MaxLength = 8;
            this.txtMobile.Properties.MaxLength = 16;
            this.txtPhone.Properties.MaxLength = 16;
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtCode.Text = String.Empty;
            this.txtName.Text = String.Empty;
            this.txtMobile.Text = String.Empty;
            this.txtPhone.Text = String.Empty;
            this.txtAddress.Text = String.Empty;
            this.txtEmail.Text = String.Empty;
            this.txtFax.Text=string.Empty;
            this.txtTaxCode.Text = string.Empty;
            this.txtWebsite.Text = string.Empty;
            this.txtDescription.Text = String.Empty;
            this.dteModifiedDate.DateTime = DateTime.Now;
            this.dteModifiedDate.Properties.MaxValue = DateTime.Now;
            this.txtContactor.Text = string.Empty;
            this.txtContactorEmail.Text = string.Empty;
            this.txtContactorMobile.Text = string.Empty;
            this.txtContactorPhone.Text = string.Empty;
            this.txtName.Focus();
        }
        #endregion

        #region Validate
        private void txtLiabilites_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtLiabilites.Text.Trim() != String.Empty)
            {
                this.dxepLiabilities.ClearErrors();
            }
        }
        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", ErrorType.User1);
            }
        }

        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.customerManager.HasCodeDuplicated(this.txtName.Text.Trim(), this.Customer.Id))
                {
                    this.dxepCode.SetError(this.txtCode, "Mã khách hàng " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã khách hàng khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtCode.Focus();
                    return;
                }

                this.dxepCode.ClearErrors();
            }
        }
        private void txtCode_Leave(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Trim() == String.Empty)
            {
                this.dxepCode.SetError(this.txtCode, "Mã khách hàng không được để trống.", ErrorType.User1);
            }
        }


        private void txtMobile_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtMobile.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.customerManager.HasMobileDuplicated(this.txtMobile.Text.Trim(), this.Customer.Id))
                {
                    this.dxepMobile.SetError(this.txtMobile, "Số điện thoại " + this.txtMobile.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtMobile.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }

        private void txtEmail_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtEmail.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                if (this.customerManager.HasEmailDuplicated(this.txtEmail.Text.Trim(), this.Customer.Id))
                {
                    this.dxepEmail.SetError(this.txtEmail, "Email " + this.txtEmail.Text.Trim() + " đã tồn tại trong hệ thống. Vui lòng kiểm tra lại.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                    this.txtEmail.Focus();
                    return;
                }

                this.dxepMobile.ClearErrors();
            }
        }


        #endregion

        #region button_click

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //id = this.Customer.Id;
            var customer = new Customer();

            // Validation
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }

            if (this.txtCode.Text.Trim().Length == 0)
            {
                this.dxepCode.SetError(this.txtCode, "Mã khách hàng không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtCode.Focus();
                return;
            }
            else if (this.customerManager.HasCodeDuplicated(this.txtCode.Text.Trim(), Customer.Id) == true)
            {
                this.dxepName.SetError(this.txtCode, "Mã khách hàng " + this.txtCode.Text.Trim() + " đã tồn tại trong hệ thống. Hãy chọn một mã khách hàng khác.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepCode.ClearErrors();
            }
            if (this.txtLiabilites.Text.Trim() == String.Empty)
            {
                this.dxepLiabilities.SetError(this.txtLiabilites, "Nhập công nợ");
                this.txtLiabilites.Focus();
                return;
            }
            else
            {
                this.dxepLiabilities.ClearErrors();
            }

            if (this.txtModifiedBy.Text.Trim().Length == 0)
            {
                this.dxepModifiedBy.SetError(this.txtModifiedBy, "Lựa chọn tên nhân viên sửa đổi");
                this.txtModifiedBy.Focus();
                return;
            }
            // End of validation
            /*****************************************************************/
            // Save into database
            customer.Id = this.Customer.Id;
            customer.Code = this.txtCode.Text.Trim();
            customer.Liabilities = float.Parse(this.txtLiabilites.Text.Trim());
            customer.Name = this.txtName.Text.Trim();
            customer.Mobile = this.txtMobile.Text.Trim();
            customer.Phone = this.txtPhone.Text.Trim();
            customer.Address = this.txtAddress.Text.Trim();
            customer.Email = this.txtEmail.Text.Trim();
            customer.Fax = this.txtFax.Text.Trim();
            customer.Website = this.txtWebsite.Text.Trim();
            customer.TaxCode = this.txtTaxCode.Text.Trim();
            customer.Description = this.txtDescription.Text.Trim();

            customer.Status = 1;

            customer.CreatedBy = this.Customer.CreatedBy;
            customer.CreatedDate = DateTime.Now;
            customer.ModifiedBy = this.txtModifiedBy.Text;
            customer.ModifiedDate = this.dteModifiedDate.DateTime;
            customer.Contactor = this.txtContactor.Text.Trim();
            customer.ContactorEmail = this.txtContactorEmail.Text.Trim();
            customer.ContactorMobile = this.txtMobile.Text.Trim();
            customer.ContactorPhone = this.txtPhone.Text.Trim();
            this.customerManager.UpdateCustomer(customer);
            XtraMessageBox.Show("Sửa dữ liệu thành công");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion

        

        

   
    }
}
